
package robotinterface.list.listeners;

import java.awt.datatransfer.*;
import java.awt.dnd.*;
import robotinterface.list.*;
import robotinterface.list.entry.*;

/**
 *
 * @author Brian Bailey
 */
public class ListTransferListener implements DragSourceListener, DragGestureListener{

    StringSelection transferable;
    DragGestureRecognizer dgr;
    DragSource ds;

    RobotList list;
    Entry entry;

    public ListTransferListener( RobotList list)
    {
        this.list = list;
        setGestureRecognizer();
    }

    private void setGestureRecognizer() {
        ds = new DragSource();
        dgr = ds.createDefaultDragGestureRecognizer(list,DnDConstants.ACTION_MOVE, this);
    }

    @Override
    public void dragEnter( DragSourceDragEvent dsde )
    {

    }

    @Override
    public void dragOver( DragSourceDragEvent dsde )
    {

    }

    @Override
    public void dropActionChanged( DragSourceDragEvent dsde )
    {

    }

    @Override
    public void dragExit( DragSourceEvent dse )
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    @Override
    public void dragDropEnd( DragSourceDropEvent dsde )
    {
        if ( dsde.getDropSuccess() ) {
            System.out.println( "Succeeded" );
        }
        else {
            System.out.println( "Failed" );
        }
    }

    @Override
    public void dragGestureRecognized( DragGestureEvent dge )
    {
        int index = list.getSelectedIndex();
        entry = (Entry)list.getModel().getElementAt(index);

        transferable = new StringSelection( entry.getTitle() );
        ds.startDrag( dge, DragSource.DefaultCopyDrop, transferable, this );
    }

    public Entry getEntry()
    {
        return entry;
    }

}
